from gui.uis.windows.main_window.setup_main_window import SetupMainWindow
from qt_core import *
from gui.core.json_themes import Themes
# IMPORT SETTINGS
# ///////////////////////////////////////////////////////////////
from gui.core.json_settings import Settings
from gui.widgets.py_window.py_window import PyWindow
from gui.widgets.py_title_bar.py_title_bar import *

class PyDialog(QDialog):
    def __init__(
        self,
        parent,
        layout=Qt.Vertical,
        margin=0,
        spacing=2,
    ):
        super().__init__()
        self.parent = parent
        self.layout = layout
        self.margin = margin
        self.spacing = spacing
        settings = Settings()
        self.settings = settings.items

        # LOAD THEME COLOR
        # ///////////////////////////////////////////////////////////////
        themes = Themes()
        self.themes = themes.items

        # ADD LAYOUT
        # ///////////////////////////////////////////////////////////////
        if layout == Qt.Vertical:
            # VERTICAL LAYOUT
            self.layout = QHBoxLayout(self)
        else:
            # HORIZONTAL LAYOUT
            self.layout = QHBoxLayout(self)

        self.layout.setContentsMargins(margin, margin, margin, margin)
        self.layout.setSpacing(spacing)
        self.setWindowFlag(Qt.FramelessWindowHint) 
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.InitUI()

    def InitUI(self):
        self.window = PyWindow(
            self,
            bg_color=self.themes["app_color"]["bg_one"],
            border_color=self.themes["app_color"]["bg_two"],
            text_color=self.themes["app_color"]["text_foreground"]
        )
        if not self.settings["custom_title_bar"]:
            self.window.set_stylesheet(border_radius = 0, border_size = 0)
       
        self.content_frame = QFrame()
        self.title_bar_frame = QFrame()
        self.title_bar_frame.setMinimumHeight(40)
        self.title_bar_frame.setMaximumHeight(40)
        self.title_bar = PyTitleBar(
            self,
            logo_width=150,
            app_parent=self,
            logo_image="logo.png",
            bg_color=self.themes["app_color"]["bg_two"],
            div_color=self.themes["app_color"]["bg_three"],
            btn_bg_color=self.themes["app_color"]["bg_two"],
            btn_bg_color_hover=self.themes["app_color"]["bg_three"],
            btn_bg_color_pressed=self.themes["app_color"]["bg_one"],
            icon_color=self.themes["app_color"]["icon_color"],
            icon_color_hover=self.themes["app_color"]["icon_hover"],
            icon_color_pressed=self.themes["app_color"]["icon_pressed"],
            icon_color_active=self.themes["app_color"]["icon_active"],
            context_color=self.themes["app_color"]["context_color"],
            dark_one=self.themes["app_color"]["dark_one"],
            text_foreground=self.themes["app_color"]["text_foreground"],
            radius=8,
            font_family=self.settings["font"]["family"],
            title_size=self.settings["font"]["title_size"],
            is_custom_title_bar=self.settings["custom_title_bar"]
        )
        self.title_bar_layout = QVBoxLayout(self.title_bar_frame)
        self.title_bar_layout.setContentsMargins(0, 0, 0, 0)
        self.title_bar_layout.addWidget(self.title_bar)
        self.content_layout = QVBoxLayout(self.content_frame)
        self.content_layout.setContentsMargins(3, 3, 3, 3)
        self.content_layout.setSpacing(6)
        self.content_area_frame = QFrame()
        self.content_area_frame.setMinimumHeight(200)
        self.content_area_layout = QVBoxLayout(self.content_area_frame)
        self.content_area_layout.setContentsMargins(0, 0, 0, 0)
        self.content_area_layout.setSpacing(0)
        self.content_layout.addWidget(self.title_bar_frame)
        self.content_layout.addWidget(self.content_area_frame)
        self.window.layout.addWidget(self.content_frame)
        self.layout.addWidget(self.window)

    def SetContentWidget(self,widget):
        self.content_area_layout.addWidget(widget)

    def btn_released(self):
        # GET BT CLICKED
        btn = SetupMainWindow.setup_btns(self)
        # DEBUG
        print(f"Button {btn.objectName()}, released!")
        
    # MOUSE CLICK EVENTS
    # ///////////////////////////////////////////////////////////////
    def mousePressEvent(self, event):
        # SET DRAG POS WINDOW
        self.dragPos = event.globalPos()
